#!/bin/bash

# 1. 检索未挂载的块设备
unmounted_devices=$(lsblk -no NAME,FSTYPE,MOUNTPOINT | awk '$2!="" && $3=="" {print "/dev/"$1}')
if [ -z "$unmounted_devices" ]; then
    echo "没有发现未挂载的磁盘。"
    exit 0
fi
echo "未挂载的磁盘列表："
echo "$unmounted_devices"

for device in $unmounted_devices; do
    index=1
    while true; do
        # 2. 生成挂载点名称
        mount_point="/mnt/disk_$index"
        # 3. 检查挂载点是否存在
        if [ ! -d "$mount_point" ]; then
            break
        fi
        ((index++))
    done

    # 4. 创建挂载点
    mkdir -p "$mount_point"

    # 5. 挂载设备
    if mount $device $mount_point; then
        echo "成功将 $device 挂载到 $mount_point"
        # 6. 获取文件系统类型
        fs_type=$(blkid -o value -s TYPE $device)
        # 7. 将挂载信息写入 /etc/fstab 实现开机自动挂载
        echo "$device $mount_point $fs_type defaults 0 0" >> /etc/fstab
        echo "已将 $device 的挂载信息添加到 /etc/fstab，系统开机时将自动挂载"
    else
        echo "无法将 $device 挂载到 $mount_point"
    fi
done
    